package com.xjj.service.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.xjj.entity.Advice;
import com.xjj.entity.PageBean;
import com.xjj.entity.Parent;
import com.xjj.entity.ResponseEntity;
import com.xjj.service.dto.AdviceDto;
import com.xjj.service.enumeration.PublishRange;
import com.xjj.service.exception.VertifyException;
import com.xjj.service.feign.KlassFeignClient;
import com.xjj.service.service.AdviceService;
import com.xjj.service.vo.AdviceVo;
import com.xjj.util.utils.JwtUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import util.GetCurrentTime;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.util.Map;

/**
 * @Description 通知
 * @Author zhangjin
 * @Date 2022/10/20 10:32
 */
@RestController
public class AdviceController {
    @Autowired
    private AdviceService adviceService;

    @Autowired
    private KlassFeignClient klassFeignClient;

    @PostMapping("/advice/admin/add")
    public ResponseEntity addAdvice(@RequestBody Advice advice){
        String currentTime = GetCurrentTime.getCurrentTime();
        advice.setTime(currentTime);
        advice.setPubRange(PublishRange.getValue(advice.getPubRange()));
        // 远程调用，通过班级名称获取班级id
        if (!StringUtils.isEmpty(advice.getClassname())){
            ResponseEntity result = klassFeignClient.isExist(advice.getClassname());
            if (result.getCode()==200){
                Map map = (Map) result.getData();
                advice.setKlassId((Integer) map.get("id"));
            }
        }
        adviceService.add(advice);
        return ResponseEntity.SUCCESS("添加成功",null);
    }

    @ApiOperation("通知列表以及我的发布")
    @PostMapping("/advice/admin/pages")
    public ResponseEntity selectMutiCondition(@RequestBody AdviceVo adviceVo){
        PageBean<Advice> result = adviceService.selectByPages(adviceVo.getPageBean(), adviceVo.getAdvice());
        for (Advice datum : result.getData()) {
            if (!StringUtils.isEmpty(datum.getClassname())){
                datum.setPubRange(datum.getClassname());
            }
        }
        return ResponseEntity.SUCCESS("查询成功",result);
    }

    @PostMapping("/advice/admin/update")
    public ResponseEntity updateAdvice(@RequestBody Advice advice){
        adviceService.update(advice);
        return ResponseEntity.SUCCESS("修改成功",null);
    }

    @GetMapping("/advice/admin/get/{id}")
    @ApiOperation("通知详情")
    public ResponseEntity getAdviceById(@PathVariable("id") Integer id){
        if (id==null){
            throw new VertifyException("id必须存在");
        }
        return ResponseEntity.SUCCESS("操作成功",adviceService.getAdviceById(id));
    }

    @GetMapping("/advice/readNum/{id}")
    @ApiOperation("查看通知阅读总次数")
    public ResponseEntity getAdviceReadNum(@PathVariable("id") Integer id){
        if (id==null){
            throw new VertifyException("id必须存在");
        }
        return ResponseEntity.SUCCESS("操作成功",adviceService.getAdviceReadNum(id));
    }

    @GetMapping("/advice/admin/census/{id}")
    @ApiOperation("统计通知阅读情况")
    public ResponseEntity getAdviceKlass(@PathVariable("id") Integer id){
        if (id==null){
            throw new VertifyException("id必须存在");
        }
        return ResponseEntity.SUCCESS("操作成功",adviceService.getClassReadNum(id));
    }

    @PostMapping("/advice/admin/getcensusStu")
    public ResponseEntity getStuCensus(@RequestBody AdviceVo adviceVo){
        return ResponseEntity.SUCCESS("操作成功",adviceService.getStuReadAdvice(adviceVo.getPageBean(),adviceVo.getAdvice()));
    }

    @GetMapping("/advice/api/getAll")
    @ApiOperation("查询全校通知")
    public ResponseEntity getParentAdvice(){
        return ResponseEntity.SUCCESS("操作成功",adviceService.getParentAdvice());
    }

    @GetMapping("/advice/api/getClassAdvice")
    @ApiOperation("查询班级通知")
    public ResponseEntity getClassAdvice(HttpServletRequest request) throws JsonProcessingException {
        String token = request.getHeader("token");
        Parent parent = JwtUtil.getParent(token);
        return ResponseEntity.SUCCESS("操作成功",adviceService.getClassAdvice(parent.getId()));
    }


    @GetMapping("/advice/api/get/{id}")
    @ApiOperation("通知详情")
    public ResponseEntity getAdviceByIdApp(@PathVariable("id") Integer id) throws ParseException {
        if (id==null){
            throw new VertifyException("id必须存在");
        }
        AdviceDto advice = adviceService.getAdviceById(id);
        advice.setTime(GetCurrentTime.timeConversion(advice.getTime()));
        return ResponseEntity.SUCCESS("操作成功",advice);
    }
}
